package com.ymatou.shop.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

/**
 * Some helper functions for the network
 */
public final class NetworkUtil {
	
	public static final int NETWORK_UNKNOW = 0;
	public static final int NETWORK_WIFI = 1;
	public static final int NETWORK_2G = 2;
	public static final int NETWORK_3G = 3;
	
	/**
     * Returns whether the network is available.
     */
	public static boolean isNetworkAvailable(Context context){
		ConnectivityManager connectivity = 
				(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			GlobalUtil.logD("couldn't get connectivity manager");
			return false;
		}

		NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();
        if (activeInfo == null) {
        	GlobalUtil.logD("network is not available");
            return false;
        }
		
    	return true;
    }
	
	/**
     * Returns whether the network is roaming.
     */
	public static boolean isNetworkRoaming(Context context) {
		ConnectivityManager connectivity =
				(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			GlobalUtil.logD("couldn't get connectivity manager");
            return false;
		}

		NetworkInfo info = connectivity.getActiveNetworkInfo();
		if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) {
			TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			if (telManager != null && telManager.isNetworkRoaming()) {
				GlobalUtil.logD("network is roaming");
				return true;
			}
		}
		
		return false;
    }
	
	/**
     * Returns whether the network is wifi connection.
     */
	public static boolean isWifiNetworkAvailable(Context context) {
		ConnectivityManager connectivity =
				(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			GlobalUtil.logD("couldn't get connectivity manager");
            return false;
		}
		
		NetworkInfo info = connectivity.getActiveNetworkInfo();
		if (info == null || !info.isConnected()) {
			return false;
		}

		if (info.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}

		return false;
	}
	
	/**
	 * Returns Network Connection type.
	 */
	public static int getNetworkType(Context context) {
		if (isWifiNetworkAvailable(context)) {
			return NETWORK_WIFI;
		}
		
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm != null) {
			switch (tm.getNetworkType()) {
			case TelephonyManager.NETWORK_TYPE_UMTS: /*UMTS (3G)*/
			case TelephonyManager.NETWORK_TYPE_HSDPA: /*HSDPA (3G - Transitional)*/
			case TelephonyManager.NETWORK_TYPE_HSUPA: /*HSUPA (3G - Transitional)*/
			case TelephonyManager.NETWORK_TYPE_HSPA: /*HSPA (3G - Transitional)*/
			case TelephonyManager.NETWORK_TYPE_EVDO_0: /*EVDO revision 0 (3G)*/
			case TelephonyManager.NETWORK_TYPE_EVDO_A: /*EVDO revision A (3G - Transitional)*/
			/*using literals to make backwards compatible*/
			case 12: //NETWORK_TYPE_EVDO_B /*EVDO revision B (3G - Transitional)*/
				return NETWORK_3G;
			
			case TelephonyManager.NETWORK_TYPE_IDEN: /*iDen (2G)*/
			case TelephonyManager.NETWORK_TYPE_1xRTT: /*1xRTT  (2G - Transitional)*/
			case TelephonyManager.NETWORK_TYPE_GPRS: /*GPRS (2.5G)*/
			case TelephonyManager.NETWORK_TYPE_EDGE: /*EDGE (2.75G)*/
			case TelephonyManager.NETWORK_TYPE_CDMA: /*CDMA: Either IS95A or IS95B (2G)*/
				return NETWORK_2G;
			}
		}
		
		return NETWORK_UNKNOW;
	}
	
	public static String getWifiName(Context context) {
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		if (wifiManager == null) {
			GlobalUtil.logD("couldn't get wifi manager");
            return StringUtil.EMPTY_STRING;
		}
        WifiInfo wInfo = wifiManager.getConnectionInfo();
        return wInfo == null ? StringUtil.EMPTY_STRING : wInfo.getSSID();
	}
}
